package com.shopping.dao;

import com.shopping.dao.provider.UpdateSelectiveProvider;
import com.shopping.pojo.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Mapper
public interface UserDao {

    String TABLE_NAME = "user";
    String INSERT_FIELDS = "avatar, level, nick_name, openid";
    String SELECT_FIELDS = "id, avatar, level, nick_name nickName, openid";

    @Insert({"insert into ", TABLE_NAME, "(",INSERT_FIELDS,")","values(#{avatar}, #{level}, #{nickMame}, #{openid})"})
    int insert(User user);

    @Select({"select ", SELECT_FIELDS, " from ", TABLE_NAME, "where id = #{id}"})
    User selectById(@Param("id") int id);

    @Select({"select nick_name nickName from ", TABLE_NAME, " where id=#{id}"})
    String selectNameById(@Param("id") int id);

    @Select({"select ", SELECT_FIELDS, " from ", TABLE_NAME, "where openid = #{openid}"})
    User selectByOpenid(@Param("openid") String openid);

    @Select({"select last_insert_id()"})
    Integer lastInsertId();

    @UpdateProvider(type = UpdateSelectiveProvider.class, method = "userUpdateSelective")
    void updateSelective(User user);

}
